package com.jjsc.client.controller;

import com.jjsc.client.service.ISmsService;
import com.jjsc.common.core.web.controller.BaseController;
import com.jjsc.common.core.web.domain.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-10-17
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping
public class SmsController extends BaseController {
    @Autowired
    private ISmsService smsService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "发送短信")
    @GetMapping("/sendCode/{phone}")
    public AjaxResult send(
            @Parameter(name = "phone", description = "手机号", required = true)
            @PathVariable("phone") String phone) {
        String code = new DecimalFormat("00000").format(new Random().nextInt(100000));
        stringRedisTemplate.opsForValue().set("phone:code:" + phone, code, 5, TimeUnit.MINUTES);
        smsService.send(phone, code);
        return success();
    }
}
